{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "from KNN.lr import LinearRegression3\n",
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-15.207930445572337"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bst=datasets.load_boston()\n",
    "X=bst.data\n",
    "y=bst.target\n",
    "std=StandardScaler()\n",
    "std.fit(X)\n",
    "X=std.transform(X)\n",
    "X_train,X_test,y_train,y_test=train_test_split(X,y)\n",
    "lr3=LinearRegression3()\n",
    "lr3.fit_sgd(X_train,y_train)\n",
    "lr3.score(X_test,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 梯度下降法的优势"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成1000个5000特征的样本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m=1000\n",
    "n=5000\n",
    "\n",
    "big_x = np.random.normal(size=(m,n))\n",
    "#这个是回归的theta的真实值\n",
    "true_theta = np.random.uniform(size=n+1)\n",
    "big_y=big_x.dot(true_theta[1:])+np.random.normal(size=m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dJ_sgd(X_b_i,y_train,theta):\n",
    "    return X_b_i.T.dot(X_b_i.dot(theta) - y_train) * 2\n",
    "def grediant_vector(X_b,y_train,theta,n_iter=1000):\n",
    "    m = 50\n",
    "    n = 5\n",
    "    i_iter = 0\n",
    "    def learning_rate(i_iter):\n",
    "        return n / (i_iter + m)\n",
    "\n",
    "    for i_iter in range(n_iter):\n",
    "        idx=np.random.randint(len(X_b))\n",
    "        theta = theta - learning_rate(i_iter) * dJ_sgd(X_b[idx],y_train[idx],theta)\n",
    "    return theta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_b = np.hstack([np.ones(shape=(len(X_train),1)),X_train])\n",
    "theta = np.zeros(shape=X_train.shape[1]+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "grediant_vector(X_b,y_train,theta,n_iter=len(X_b) * 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
